package at.ac.univie.zsu.aguataplan.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.springframework.format.annotation.DateTimeFormat;

/**
 * @author gerry
 * 
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Tournament {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	protected Long id;

	@Version
	@Column
	protected Integer version;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "M-")
	@Column
	protected Date calDate;

	@Column(nullable = false, unique = true)
	protected String description;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	protected Set<Video> refVideo = new HashSet<Video>();

	@ManyToOne
	protected TournamentLocation refTournamentLocation;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Tournament() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public boolean addVideo(Video video) {
		return this.refVideo.add(video);
	}

	public boolean deleteVideo(long id) {
		Iterator<Video> it = refVideo.iterator();

		long j = -1;

		while (it.hasNext()) {
			Video v = it.next();
			j = v.getId();
			if (id == j) {
				it.remove();
				return true;
			}
		}

		return false;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("CalDate: ").append(getCalDate()).append(", ");
		sb.append("Description: ").append(getDescription()).append(", ");
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("RefTournamentLocation: ").append(getRefTournamentLocation()).append(", ");
		sb.append("RefVideo: ").append(getRefVideo() == null ? "null" : getRefVideo().size()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Date getCalDate() {
		return calDate;
	}

	public void setCalDate(Date calDate) {
		this.calDate = calDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<Video> getRefVideo() {
		return refVideo;
	}

	public void setRefVideo(Set<Video> refVideo) {
		this.refVideo = refVideo;
	}

	public TournamentLocation getRefTournamentLocation() {
		return refTournamentLocation;
	}

	public void setRefTournamentLocation(TournamentLocation refTournamentLocation) {
		this.refTournamentLocation = refTournamentLocation;
	}

}
